Types
A data type describes how the information of a variable is stored in memory. For example, a variable can hold a floating point number or a string in the legacy Sinclair BASIC. But sometimes this is a waste of time and memory. For example, why don't we use a single byte to store a value we know it's always an integer between 0 and 10? Now you can: ZX BASIC allows more data types to save memory and achieve higher speed.
There are 3 kinds of types: integers, decimals and strings
Integers are a numeric type. They can be unsigned (their value is always 0 or positive) or signed (can take negative values). ZX Basic integer types sizes are 8, 16 and 32 bits. Unsigned types have the prefix U.
Integer types
Identifier |
Size |
Signed? |
Range |
Description |
---|---|---|---|---|
Byte |
1 |
Yes |
-128..127 |
8 bits signed integer |
UByte |
1 |
No |
0..255 |
8 bits unsigned integer |
Integer |
2 |
Yes |
-32768..32767 |
16 bits signed integer |
UInteger |
2 |
No |
0..65535 |
16 bits unsigned integer |
Long |
4 |
Yes |
−2,147,483,648 .. +2,147,483,647 |
32 bits signed integer |
ULong |
4 |
No |
0 .. 4,294,967,295 |
32 bits unsigned integer |
Decimals, as suggested, stores decimal numeric types. Their sizes are 32 bit for Fixed type and 40 bytes for Float one.
32 bit Fixed Point decimal. First 16 bits are the integer part, whilst remaining 16 contains the decimal one. Ranges from -32767.9999847 to 32767.9999847 with a precision of 1 / 2^16 (0.000015 aprox.). Fixed points decimal are less precise than Floating ones, but much faster and requires less space (1 byte less). Also, their range is much limited. They're usually used on screen drawing when Floating point is too slow and decimal calculations are required.
Floating point type is identical to the Sinclair BASIC one. It requires 5 bytes (1 byte for exponent, 4 bits for mantissa). Read the ZX Spectrum manual.
To store the number in the computer, we use five bytes, as follows:
write the first eight bits of the mantissa in the second byte (we know that the first bit is 1), the second eight bits in the third byte, the third eight bits in the fourth byte and the fourth eight bits in the fifth byte;
replace the first bit in the second byte which we know is 1 by the sign: 0 for plus, 1 for minus;
write the exponent +128 in the first byte. For instance, suppose our number is 1 / 10, then
1 / 10 = (4 / 5) * 2 ^ (-3)
String types are used to store alphanumerical strings. Strings can contain up to 65535 characters (depending on available free memory), and they can change its size dynamically so, unlike other data types, their content is stored in a different memory area, called the Heap
In most BASIC dialects, string variables used to have the $ suffix (also called sigil), but suffixes are optional in ZX BASIC (you can omit them).
Supported Plattforms:
All